package cn.psvmc.cxzapi.controller.ticket;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.MarketEntity;
import cn.psvmc.cxzapi.model.ticket.TTicketCause;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.ticket.TTicketCauseService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 工单原因
 * @Author auto
 * @Date 2024-08-12
 */
@RestController
@RequestMapping("/ticket_cause")
public class TTicketCauseController {

    @Resource
    private TTicketCauseService tTicketCauseService;

    /**
    * @Author auto 2024-08-12 新增工单原因表
    * @Param tTicketCause 工单原因表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TTicketCause tTicketCause) {
        int addResult = tTicketCauseService.add(tTicketCause);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-12 更新工单原因表
    * @Param tTicketCause 工单原因表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TTicketCause tTicketCause) {
        ToolUtils.validation(new Object[]{tTicketCause.getTcid(), "id", 1, tTicketCause.getRversion(), "版本", 1});
        int updateResult = tTicketCauseService.edit(tTicketCause);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-12 删除工单原因表
    * @Param [entity]
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tTicketCauseService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-12 管理列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public Result list(@RequestBody MarketEntity entity) {
        Integer typeId = entity.getTypeId();
        ToolUtils.validation(new Object[]{typeId, "工单类型id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", tTicketCauseService.listByTypeId(typeId));
    }

    /**
     * @Description 选择列表
     * @Date 2024/8/12 22:54
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public Result listForSel(@RequestBody MarketEntity entity) {
        Integer typeId = entity.getTypeId();
        ToolUtils.validation(new Object[]{typeId, "工单类型id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", tTicketCauseService.listByTypeIdForSel(typeId));
    }
}
